home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / util / ArchivingStack.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  3.6 KB  |  120 lines  |  [TEXT/CWIE]

  1. // ArchivingStack.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.util;
  6.  
  7. /** This private class is used by the Archiver and Unarchiver to maintain
  8.   * the stack of states associated with archiving and unarchiving a graph
  9.   * of objects. The ArchivingStack tries to be efficient in space and time
  10.   * so some of the stuff here is gross, but at least it's private. The two
  11.   * hacks which are important to know about are:
  12.   *
  13.   * 1. The parts of the Archiver/Unarchiver state which are Objects are packed
  14.   * into one array, and the parts which are ints are all packed into another.
  15.   * Don't mess up the multiples and offsets!
  16.   *
  17.   * 2. Because this has to reach into both Archivers and Unarchivers there
  18.   * are separate push and pop methods for each to satisfy the type system.
  19.   * Do not mix and match!
  20.   */
  21. final class ArchivingStack {
  22.     int depth;
  23.     int maxDepth;
  24.     Object objArray[];
  25.     int intArray[];
  26.  
  27.     ArchivingStack() {
  28.         super();
  29.     }
  30.  
  31.     private void growArrays() {
  32.         Object newObjArray[];
  33.         int newIntArray[];
  34.  
  35.         if (maxDepth == 0)
  36.             maxDepth = 8;
  37.         else
  38.             maxDepth = 2 * maxDepth;
  39.  
  40.         newObjArray = new Object[2 * maxDepth];
  41.         newIntArray = new int[4 * maxDepth];
  42.  
  43.         if (objArray != null && intArray != null) {
  44.             System.arraycopy(objArray, 0, newObjArray, 0, objArray.length);
  45.             System.arraycopy(intArray, 0, newIntArray, 0, intArray.length);
  46.         }
  47.  
  48.         objArray = newObjArray;
  49.         intArray = newIntArray;
  50.     }
  51.  
  52.     void pushArchiver(Archiver archiver) {
  53.         int i;
  54.  
  55.         depth++;
  56.         i = depth;
  57.  
  58.         if (depth >= maxDepth)
  59.             growArrays();
  60.  
  61.         objArray[2 * i + 0] = archiver.currentObject;
  62.         objArray[2 * i + 1] = archiver.currentTable;
  63.  
  64.         intArray[4 * i + 0] = archiver.currentId;
  65.         intArray[4 * i + 1] = archiver.currentColumnCount;
  66.         intArray[4 * i + 2] = archiver.currentRow;
  67.         intArray[4 * i + 3] = archiver.currentColumn;
  68.     }
  69.  
  70.     void pushUnarchiver(Unarchiver unarchiver) {
  71.         int i;
  72.  
  73.         depth++;
  74.         i = depth;
  75.  
  76.         if (depth >= maxDepth)
  77.             growArrays();
  78.  
  79.         objArray[2 * i + 0] = unarchiver.currentObject;
  80.         objArray[2 * i + 1] = unarchiver.currentTable;
  81.  
  82.         intArray[4 * i + 0] = unarchiver.currentId;
  83.         intArray[4 * i + 1] = unarchiver.currentColumnCount;
  84.         intArray[4 * i + 2] = unarchiver.currentRow;
  85.         intArray[4 * i + 3] = unarchiver.currentColumn;
  86.     }
  87.  
  88.     void popArchiver(Archiver archiver) {
  89.         int i = depth;
  90.  
  91.         archiver.currentObject = objArray[2 * i + 0];
  92.         objArray[2 * i + 0] = null;
  93.         archiver.currentTable  = (ClassTable)objArray[2 * i + 1];
  94.         objArray[2 * i + 1] = null;
  95.  
  96.         archiver.currentId          = intArray[4 * i + 0];
  97.         archiver.currentColumnCount = intArray[4 * i + 1];
  98.         archiver.currentRow         = intArray[4 * i + 2];
  99.         archiver.currentColumn      = intArray[4 * i + 3];
  100.  
  101.         depth--;
  102.     }
  103.  
  104.     void popUnarchiver(Unarchiver unarchiver) {
  105.         int i = depth;
  106.  
  107.         unarchiver.currentObject = objArray[2 * i + 0];
  108.         objArray[2 * i + 0] = null;
  109.         unarchiver.currentTable  = (ClassTable)objArray[2 * i + 1];
  110.         objArray[2 * i + 1] = null;
  111.  
  112.         unarchiver.currentId          = intArray[4 * i + 0];
  113.         unarchiver.currentColumnCount = intArray[4 * i + 1];
  114.         unarchiver.currentRow         = intArray[4 * i + 2];
  115.         unarchiver.currentColumn      = intArray[4 * i + 3];
  116.  
  117.         depth--;
  118.     }
  119. }
  120.